package tests.第三方工具类测试使用.htmlunit;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author: SunAo
 * @create: 2020-09-12 14:46
 */
public class 简单示例 {


    @Test
    public void 获取百度首页() throws Exception {
        try (final WebClient webClient = new WebClient()) {
            final HtmlPage page = webClient.getPage("http://www.baidu.com");
            Assert.assertEquals("百度一下，你就知道",page.getTitleText());
        }
    }

    @Test
    public void 提交一个表单() throws Exception {
        try (final WebClient webClient = new WebClient()) {
            // Get the first page
            final HtmlPage page1 = webClient.getPage("http://some_url");
            // Get the form that we are dealing with and within that form,
            // find the submit button and the field that we want to change.
            final HtmlForm form = page1.getFormByName("myform");
            final HtmlSubmitInput button = form.getInputByName("submitbutton");
            final HtmlTextInput textField = form.getInputByName("userid");
            // Change the value of the text field
            textField.type("root");
            // Now submit the form by clicking the button and get back the second page.
            final HtmlPage page2 = button.click();
        }
    }

}
